package ExtendUtils

import (
	"gitee.com/fierce_wolf/go-fox-edge-common/commUtil/Pair"
	"gitee.com/fierce_wolf/go-fox-edge-common/commUtil/String"
)

func Extend(mapList []map[string]interface{}, extendList []map[string]interface{}, pairs []*Pair.Pair, property []string) {
	if mapList == nil {
		return
	}

	// 重新组织扩展信息，作为下一步的字典
	indexMap := make(map[string]map[string]interface{})
	for _, mp := range extendList {
		serviceKeys := make([]interface{}, 0)

		for _, key := range pairs {
			value := mp[key.Value.(string)]
			if value == nil {
				continue
			}

			serviceKeys = append(serviceKeys, value)
		}

		if len(serviceKeys) != len(pairs) {
			continue
		}

		key := String.ToString(serviceKeys)
		indexMap[key] = mp
	}

	for _, mp := range mapList {
		serviceKeys := make([]interface{}, 0)

		for _, pair := range pairs {
			value := mp[pair.Key.(string)]
			if value == nil {
				continue
			}

			serviceKeys = append(serviceKeys, value)
		}

		if len(serviceKeys) != len(pairs) {
			continue
		}

		key := String.ToString(serviceKeys)
		extend := indexMap[key]
		if extend == nil {
			continue
		}

		for _, p := range property {
			extendValue := extend[p]
			if extendValue == nil {
				continue
			}

			mp[p] = extendValue
		}
	}

}
